﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using VAdvantage.GlobalWeatherService;
using VAdvantage.DSProcessWorkflow;
using VAdvantage.Utility;

namespace VAdvantage.Widgets
{
    public partial class WeatherControl : UserControl
    {
        #region Global Declarations

        //GlobalWeatherSoapClient _GlobalWeatherSoapClient;
        //DSProcessWorkflowClient _SettingsServiceClient;

        #endregion

        #region Properties

        private string _RootPath;
        public string RootPath
        {
            get { return _RootPath; }
            set { _RootPath = value; }
        }

        #endregion

        public WeatherControl()
        {
            InitializeComponent();
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            TimeSpan now = DateTime.Now.TimeOfDay;
            H1.AnimateTo(DateTime.Now.ToString("hh"));
            M1.AnimateTo(DateTime.Now.ToString("mm"));
            tbAMPM.Text = DateTime.Now.ToString("tt");
        }

        #region Private Methods
        public void LoadWeather()
        {
            var _SettingsServiceClient = AEnv.GetDSPWFClient();
            _SettingsServiceClient.DeserializeFromXMLCompleted += new EventHandler<DeserializeFromXMLCompletedEventArgs>(DeserializeFromXMLCompleted);
            _SettingsServiceClient.DeserializeFromXMLAsync(Envs.GetCtx().GetAD_User_ID());
            _SettingsServiceClient.CloseAsync();
            imgWeatherStatus.Visibility = System.Windows.Visibility.Collapsed;
        }
        #endregion

        #region Service Generated Events

        private void DeserializeFromXMLCompleted(object sender, DeserializeFromXMLCompletedEventArgs e)
        {
            string location = e.Result;

            if (!string.IsNullOrEmpty(location))
            {
                string[] arrLocation = location.Split('#');
                var _GlobalWeatherSoapClient = AEnv.GetGlobalWeatherSoapClient();
                
                _GlobalWeatherSoapClient.GetWeatherCompleted += new EventHandler<GetWeatherCompletedEventArgs>(GetWeatherCompleted);
                _GlobalWeatherSoapClient.GetWeatherAsync(arrLocation[1], arrLocation[0]);
                _GlobalWeatherSoapClient.CloseAsync();
            }
        }

        private void GetWeatherCompleted(object sender, GetWeatherCompletedEventArgs e)
        {
            try
            {
                string strWeatherDetails = e.Result;
                if (!string.IsNullOrEmpty(strWeatherDetails) && strWeatherDetails != "Data Not Found")
                {
                    WeatherDetails weatherDetails = new WeatherDetails();

                    XDocument xDoc = XDocument.Parse(strWeatherDetails);
                    var currentWeather = xDoc.Root.Descendants("CurrentWeather");

                    weatherDetails.Location = xDoc.Root.Descendants("Location").First().Value;
                    weatherDetails.Time = xDoc.Root.Descendants("Time").First().Value;
                    weatherDetails.Wind = xDoc.Root.Descendants("Wind").First().Value;
                    weatherDetails.Visibility = xDoc.Root.Descendants("Visibility").First().Value;
                    weatherDetails.Temperature = xDoc.Root.Descendants("Temperature").First().Value;
                    weatherDetails.DewPoint = xDoc.Root.Descendants("DewPoint").First().Value;
                    weatherDetails.RelativeHumidity = xDoc.Root.Descendants("RelativeHumidity").First().Value;
                    weatherDetails.Pressure = xDoc.Root.Descendants("Pressure").First().Value;
                    weatherDetails.Status = xDoc.Root.Descendants("Status").First().Value;
                    //weatherDetails.SkyCondition = xDoc.Root.Descendants("SkyConditions").First().Value;

                    tbCityName.Text = weatherDetails.Location.Substring(0, weatherDetails.Location.IndexOf(","));
                    tbTemp.Text = weatherDetails.Temperature.Substring(weatherDetails.Temperature.IndexOf("(") + 1, (weatherDetails.Temperature.IndexOf(" C") - weatherDetails.Temperature.IndexOf("(")) - 1) + "°";
                    tbHTemp.Text = weatherDetails.DewPoint.Substring(weatherDetails.DewPoint.IndexOf("(") + 1, (weatherDetails.DewPoint.IndexOf(" C") - weatherDetails.DewPoint.IndexOf("(")) - 1) + "°";
                    tbLTemp.Text = weatherDetails.RelativeHumidity;
                    tbWeatherStatus.Text = "Wind: " + weatherDetails.Wind;
                }
            }
            catch 
            {
                Logging.VLogger.Get().Severe("Error gettting Weather Info");
                //MessageBox.Show("Data Not Found.");
            }
            //busyIndicator.IsBusy = false;
        }


        #endregion

        private void imgSettings_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Settings ctrlSettings = new Settings(RootPath);
            ctrlSettings.Closed += new EventHandler(Settings_Closed);
            ctrlSettings.Show();
        }

        void Settings_Closed(object sender, EventArgs e)
        {
            LoadWeather();
        }
    }
}
